package arrAndstr;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/*
    202. 快乐数
 */
public class T202 {

    public static void main(String[] args) {
        int n = 2;
        System.out.println(isHappy(n));
    }

    public static boolean isHappy(int n) {
        List<Integer> list = new ArrayList<>();

        while (true) {
            int computedNum = getComputedNum(n);
            if (list.contains(computedNum)) {
                return false;
            }
            if (computedNum==1) {
                return true;
            }
            list.add(n);
            n = computedNum;
        }
    }

    private static int getComputedNum(int n) {
        int result = 0;
        char[] chars = String.valueOf(n).toCharArray();
        for (char ch : chars) {
            result += (ch - '0') * (ch - '0');
        }
        return result;
    }
}
